<?php
namespace Client\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;

class VoucherTable extends CommonTable
{
    protected $table = 'hy_voucher';
    protected $id = 'id';
    
    public function getVoucherList($query = array())
    {
        $select = new Select(array('v'=>$this->getTable()));
        $select->columns( array(
            "*"
        ));
    
        $select->join(
            array('t' => 'hy_voucher_type'),
            'v.vt_id = t.vt_id',
            array('vt_price','vt_name','vt_limit_price','vt_start_time','vt_end_time','vt_remark','org_id','product_id') ,
            $select::JOIN_INNER
            );
        
        $select->join(
            array('o' => 'hy_organization'),
            't.org_id = o.org_id',
            array('org_name') ,
            $select::JOIN_LEFT
            );
        $select->join(
            array('p' => 'hy_product'),
            't.product_id = p.product_id',
            array('product_name') ,
            $select::JOIN_LEFT
            );
        
        $select->where(array('v.user_id'=>$query['user_id']));
    
        $select->order(array('t.vt_end_time asc'));
    
        $rowset = $this->selectWith($select);
        $list = $rowset->toArray();
    
        return $list;
    }

    public function getUseableList($query = array(),$total_price)
    {
        $select = new Select(array('v'=>$this->getTable()));
        $select->columns( array(
            "*"
        ));
    
        $select->join(
            array('t' => 'hy_voucher_type'),
            'v.vt_id = t.vt_id',
            array('vt_price','vt_name','vt_limit_price','vt_start_time','vt_end_time','vt_remark','org_id','product_id') ,
            $select::JOIN_INNER
            );
        
        $where = new Where();
        $where->lessThanOrEqualTo('vt_limit_price', $total_price);
        $select->where($where);
        
        if(!empty($query['org_id'])) {
            $select->where(array('t.org_id' => $query['org_id']));
        }  
    
        $select->where(array('v.user_id'=>$query['user_id'],'v.has_use' => 0));
    
        $select->order(array('t.vt_end_time asc'));
    
        $rowset = $this->selectWith($select);
        $list = $rowset->toArray(); 
    
        return $list;
    }
    
    public function getVoucherById($voucher_id) {
        $select = new Select(array('v'=>$this->getTable()));
        $select->columns( array(
            "*"
        ));
        
        $select->join(
            array('t' => 'hy_voucher_type'),
            'v.vt_id = t.vt_id',
            array('vt_price','vt_name','vt_limit_price','vt_start_time','vt_end_time','vt_remark','org_id','product_id') ,
            $select::JOIN_INNER
            );
        
        $select->join(
            array('o' => 'hy_organization'),
            't.org_id = o.org_id',
            array('org_name') ,
            $select::JOIN_LEFT
            );
        $select->join(
            array('p' => 'hy_product'),
            't.product_id = p.product_id',
            array('product_name') ,
            $select::JOIN_LEFT
            );
        
        $select->where(array('v.id'=>$voucher_id));

        $rowset = $this->selectWith($select);
        $row = $rowset->current();
        return $row;
    }
    
    public function getCountById($vt_id)
    {
        $rowset = $this->select(array('vt_id' => $vt_id));
        $count = $rowset->count();
        return $count;
    }
 
}